package com.xinchuang.mapper.targetSystem;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xinchuang.entity.targetSystem.system.CommonAttribute;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName CommonMapper
 * @Description: TODO(共有属性数据层)
 * @Author zhangxuan
 * @Date 2021-03-30 17:51
 * @Verion V1.0
 */
public interface CommonMapper extends BaseMapper<CommonAttribute> {

    @Insert(value = {
            "INSERT INTO target_common_attribute ",
            "(id,key_name,key_value,key_type,show_type,required,info,data_range,verify,min,max,enumerator,row_value,sign,message,sort) ",
            "VALUES ",
            "(#{id},#{keyName},#{key},#{keyType},#{showType},#{required},#{info},#{dataRange},#{verify},#{min},#{max},#{enumerator},#{row},#{sign},#{message},#{sort})",
    })
    int addCommon(CommonAttribute commonAttribute);

    @Insert(value = {
            "INSERT INTO target_common_attribute ",
            "(id,key_name,key_value,key_type,show_type,required,info,data_range,verify,min,max,enumerator,row_value,sign,message,sort) ",
            "VALUES ",
            "(#{id},#{keyName},#{key},#{keyType},#{showType},#{required},#{info},#{dataRange},#{verify},#{min},#{max},#{enumerator},#{row},#{sign},#{message},#{sort})",
            "ON DUPLICATE KEY UPDATE key_name = #{keyName}, key_value = #{key}, key_type = #{key_type}, show_type = #{show_type}, required = #{required}, info = #{info}, data_range = #{data_range}, verify = #{verify}, min = #{min}, max = #{max}, enumerator = #{enumerator}, row_value = #{row}, sign = #{sign}, message = #{message}, sort = #{sort}"
    })
    int addReplaceCommon(CommonAttribute commonAttribute);

    @Insert(value = {
            "<script>",
            "INSERT INTO target_common_attribute ",
            "(id,key_name,key_value,key_type,show_type,required,info,data_range,verify,min,max,enumerator,row_value,sign,message,sort) ",
            "VALUES ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.id},#{item.keyName},#{item.key},#{item.keyType},#{item.showType},#{item.required},#{item.info},#{item.dataRange},#{item.verify},#{item.min},#{item.max},#{item.enumerator},#{item.row},#{item.sign},#{item.message},#{item.sort})",
            "</foreach>",
            "</script>"
    })
    int addCommons(List<CommonAttribute> commonAttributes);

    @Delete(value = "DELETE FROM target_common_attribute WHERE id = #{id}")
    int deleteCommon(@Param("id") String id);

    @Update(value = {
            "<script>",
            "UPDATE target_common_attribute ",
            "<set>",
            "<if test = 'keyName != null'>key_name = #{keyName},</if>",
            "<if test = 'key != null'>key_value = #{key},</if>",
            "<if test = 'keyType != null'>key_type = #{keyType},</if>",
            "<if test = 'showType != null'>show_type = #{showType},</if>",
            "<if test = 'required != null'>required = #{required},</if>",
            "<if test = 'info != null'>info = #{info},</if>",
            "<if test = 'dataRange != null'>data_range = #{dataRange},</if>",
            "<if test = 'verify != null'>verify = #{verify},</if>",
            "<if test = 'min != null'>min = #{min},</if>",
            "<if test = 'max != null'>max = #{max},</if>",
            "<if test = 'enumerator != null'>enumerator = #{enumerator},</if>",
            "<if test = 'row != null'>row_value = #{row},</if>",
            "<if test = 'sign != null'>sign = #{sign},</if>",
            "<if test = 'message != null'>message = #{message},</if>",
            "<if test = 'sort != null'>sort = #{sort},</if>",
            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateCommon(CommonAttribute commonAttribute);

    @Select(value = {
            "SELECT id,key_name,key_value,key_type,show_type,required,info,data_range,verify,min,max,enumerator,row_value,sign,message,sort FROM target_common_attribute WHERE sign = #{sign} ORDER BY sort ASC"
    })
    @Results(id = "commonResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "keyName", column = "key_name"),
            @Result(property = "key", column = "key_value"),
            @Result(property = "keyType", column = "key_type"),
            @Result(property = "showType", column = "show_type"),
            @Result(property = "required", column = "required"),
            @Result(property = "info", column = "info"),
            @Result(property = "dataRange", column = "data_range"),
            @Result(property = "verify", column = "verify"),
            @Result(property = "min", column = "min"),
            @Result(property = "max", column = "max"),
            @Result(property = "enumerator", column = "enumerator"),
            @Result(property = "row", column = "row_value"),
            @Result(property = "sign", column = "sign"),
            @Result(property = "message", column = "message")
    })
    List<CommonAttribute> findCommons(@Param("sign") Integer sign);

    @Select(value = {
            "SELECT id,key_name,key_value,key_type,show_type,required,info,data_range,verify,min,max,enumerator,row_value,sign,message,sort FROM target_common_attribute WHERE id = #{id} LIMIT 1"
    })
    @ResultMap(value = "commonResultMap")
    CommonAttribute findOneCommonById(@Param("id")String id);

    @Select(value = {
            "SELECT id,sign,sort FROM target_common_attribute WHERE id = #{id} LIMIT 1"
    })
    CommonAttribute findCommonSort(@Param("id")String id);

    @Select(value = {
            "SELECT id,sign,sort FROM target_common_attribute WHERE sign = #{sign}"
    })
    List<CommonAttribute> findCommonSortSign(@Param("sign") Integer sign);

    @Select("SELECT message FROM target_common_attribute WHERE sign = #{sign} AND key_value = 'desicribe'")
    String findMessage(@Param("sign") Integer sign);

    @Update(value = "UPDATE target_common_attribute SET sort = #{sort} WHERE id = #{id}")
    int updateCommonSort(@Param("id")String id, @Param("sort") Integer sort);

    @Select("SELECT MAX(sort) FROM target_common_attribute WHERE sign = #{sign}")
    Integer getCommonMaxSort(@Param("sign") Integer sign);

    @Select("SELECT id,sort AS value FROM target_common_attribute WHERE sign = #{sign} ORDER BY sort ASC LIMIT 1")
    Map<String, Object> getMin(@Param("sign") Integer sign);

    @Select("SELECT id,sort AS value FROM target_common_attribute WHERE sign = #{sign} ORDER BY sort DESC LIMIT 1")
    Map<String, Object> getMax(@Param("sign") Integer sign);

    @Select("SELECT id,sort FROM target_common_attribute WHERE sort < #{sort} AND sign = #{sign} ORDER BY sort DESC LIMIT 1")
    Map<String, Object> getUp(@Param("sort") Integer sort, @Param("sign") Integer sign);

    @Select("SELECT id,sort FROM target_common_attribute WHERE sort > #{sort} AND sign = #{sign} ORDER BY sort ASC LIMIT 1")
    Map<String, Object> getDown(@Param("sort") Integer sort, @Param("sign") Integer sign);

}
